﻿package net.wangit.support.action;

import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.wangit.context.ActionContext;
import net.wangit.context.ActionContextHelper;
import net.wangit.framework.action.ActionUtil;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.QueryParameters;
import net.wangit.jdbc.table.Column;
import net.wangit.jdbc.table.TableMeta;
import net.wangit.jdbc.table.TableMetaFactory;
import net.wangit.util.Checker;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.unit.UnitFactory;


/**
 * 从blob字段下载文件。
 * 
 * @author watson Feb 25, 2010
 */
public class BlobDownloadAction extends DownloadAction {

	public BlobDownloadAction() {
	}

	// 下载接口。
	protected void doDownload(ActionContext ac, HttpServletResponse response)
			throws Exception {
		String elementId;
		ResultSet rs;
		String fileName;
		String contentType;
		OutputStream out;
		InputStream in;
		byte buffer[];
		String domain = ActionContextHelper.getActionDomain(ac);
		String unitId = ActionContextHelper.getActionUnitId(ac);
		elementId = ActionContextHelper.getActionId(ac);
		UnitComponent unit = UnitFactory.getUnitWithoutClone(domain, unitId);
		String tableName = unit.getTableName();
		if (Checker.isEmpty(tableName))
			return;
		TableMeta table = TableMetaFactory.getTableMeta(domain, tableName);
		List columns = table.getPrimaryKey().getColumns();
		String values[] = new String[columns.size()];
		int types[] = new int[columns.size()];
		StringBuffer sql = new StringBuffer("select * from ");
		sql.append(tableName).append(" where ");
		for (int i = 0; i < columns.size(); i++) {
			Column column = (Column) columns.get(i);
			String columnName = column.getName();
			if (i > 0)
				sql.append(" and ");
			sql.append(columnName).append("=?");
			values[i] = ac.getRequestParameter(columnName);
			types[i] = column.getType().code();
		}

		DB db = ActionUtil.getActionDB(ac);
		rs = db
				.getResultSet(sql.toString(),
						new QueryParameters(values, types));
		if (rs == null || !rs.next())
			return;
		fileName = rs.getString((new StringBuilder(String.valueOf(elementId)))
				.append("filename").toString());
		contentType = rs
				.getString((new StringBuilder(String.valueOf(elementId)))
						.append("contenttype").toString());
		out = null;
		in = null;
		try {
			buffer = new byte[2048];
			in = rs.getBinaryStream(elementId);
			if (in == null) {
				if (in != null)
					in.close();
				if (out != null)
					out.close();
				return;
			}
			out = getOutputStream(response, fileName, contentType);
			for (int byteCount = in.read(buffer); byteCount > 0; byteCount = in
					.read(buffer))
				out.write(buffer, 0, byteCount);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (Exception e) {
				}
			}
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}
}
